package com.example.datedemo.service.tools;

import com.example.datedemo.service.dto.ResponseResult;
import org.springframework.dao.DuplicateKeyException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class StaticTools {
    public static ResponseResult getModifyResult(int i, Object Sample) {
        try {
            int updateNum = i;
            if (updateNum == 0) {
                return new ResponseResult(0, "失败");
            }
        } catch (Exception e) {
            boolean Duplicate = e instanceof DuplicateKeyException;
            if (Duplicate) {
                return new ResponseResult(13, "此项目，此年 此周/此月 已有记录 或 主键重复");
            }
            if (Duplicate) {
                return new ResponseResult(4, "未知错误");
            }
        }
        return new ResponseResult(1, "成功");
    }

    // 月份+1纠正

    public static Date tranDate(String monthNumber) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Date tempDate = null;
        try {
            tempDate = dateFormat.parse(monthNumber);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar tempCalendar = Calendar.getInstance();
        assert tempDate != null;
        tempCalendar.setTime(tempDate);
        tempCalendar.add(Calendar.MONTH, +1);
        tempDate = tempCalendar.getTime();
        return tempDate;
    }

    public static boolean isNormalWeek(int weekNumber) {
        if (weekNumber > 6 || weekNumber <= 0) {
            return true;
        }
        return false;
    }
}
